package com.csnj.admin.controller.SystemManager;

import com.csnj.common.entity.APIResult;
import com.csnj.common.rest.BaseController;
import com.csnj.core.Srvs.SysManager.ElementSrvs;
import com.csnj.core.entity.SysElement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@RestController
@RequestMapping("element")
public class ElementController extends BaseController<ElementSrvs, SysElement> {

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public APIResult page(String name, @RequestParam(defaultValue = "0") int menuId) {
    Example example = new Example(SysElement.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("menuId", menuId);
    if(StringUtils.isNotBlank(name)){
      criteria.andLike("name", "%" + name + "%");
    }
    List<SysElement> elements = baseBiz.selectByExample(example);
    return new APIResult(elements);
  }

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  @ResponseBody
  public APIResult getAuthorityElement(Integer menuId, Integer userId) {
    List<SysElement> elements = baseBiz.getAuthorityElementByUserId(userId, menuId);
    return new APIResult(elements);
  }

  @RequestMapping(value = "/user/menu", method = RequestMethod.GET)
  @ResponseBody
  public APIResult getAuthorityElement(Integer userId) {
    List<SysElement> elements = baseBiz.getAuthorityElementByUserId(userId);
    return new APIResult(elements);
  }
}
